import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { ApiService } from '../../services/api.service'
import { Router } from '@angular/router'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'
import { DictService } from '@app/shared/utils/dict.service'
import groupEditForm from '../../../assets/formio/customer-group-edit.json'
import { ModalService } from '@app/shared/utils'
import {CursorService} from "@core/http/cursor.service";
@Component({
  selector: 'app-customer-group',
  templateUrl: './customer-group.component.html',
  styleUrls: ['./customer-group.component.css'],
  providers: [ApiService, ModalService]
})
export class CustomerGroupComponent implements OnInit {
  groupname ='';
  groupstatus ='';
  @ViewChild('searchList', { static: true })
  private searchListTemp: TemplateRef<any>;

  @ViewChild('groupItemEdit', { static: true })
  private groupItemEditTemp: TemplateRef<any>;
  groupno = '';
  public formGroup: FormGroup;
  public groupList: any[] = [];
  public edidflag: any = '';
  public customerList = [];
  attrDialog = false;
  group_state = [];
  // 弹框
  public groupForm = groupEditForm;
  public groupFormData = {
    data: {}
  }
  constructor(
    public dictService: DictService,
    private fb: FormBuilder,
    private router: Router,
    private apiService: ApiService,
    public modal: ModalService,
    private message: QlMessageService,
    public cursorService: CursorService,
  ) {}

  ngOnInit() {
    this.formGroup = this.fb.group({
      name: [''],
      state: ['']
    })
    this.onRefresh()
    // let state =[]
    //
    if(this.dictService.getDict('group_state')!=undefined){
      for(let item of this.dictService.getDict('group_state')){
        this.group_state.push({label:item['paramLabel'],value:item['paramValue']});
      }
    }
    // this.apiService.getCustomerGroupList().subscribe(data => {
      console.log('group_state:'+JSON.stringify( this.dictService.getDict('group_state')))
    //   this.customerGropupList = data.sort(x => 0.5 - Math.random())
    // })
  }

  public onRefresh() {
    this.cursorService.limit=10
    // let sql='select *'
    if(this.groupname!=''){
      this.cursorService.query().eq('groupname',this.groupname)
      // sql= this.transform('groupname',this.groupname ,sql)
    }
    if(this.groupstatus!=''){
      this.cursorService.query().eq('grouptatus',this.groupstatus)
      // sql= this.transform('grouptatus',this.groupstatus ,sql)
    }
    // this.cursorService.statement = sql;
    this.apiService.getCustomerGroupList(this.cursorService).subscribe(data => {
      this.groupList = data
    })
    this.cursorService.query({reset:true})
  }

  public editGroup(data) {
    this.groupno = data['groupno']
    this.attrDialog = true;
    this.groupForm.components[1]['columns'][1]['components'][0]['data']['values'] = this.group_state
    this.groupFormData = {
      data:{
        groupcode:data.groupcode,
        groupname:data.groupname,
        clerkname:data.clerkname,
        groupcount: data.groupcount,
        groupstatus: data.groupstatus,
        groupdescription: data.groupdescription,
      }
    }
    // this.modal
    //   .open({
    //     title: '编辑分组',
    //     component: this.groupItemEditTemp
    //   })
    //   .subscribe(() => {
    //     this.message.success('修改成功')
    //   })
  }
  cancel(){
    this.attrDialog = false;
  }
  public queryCustomer(){
    this.modal
    .open({
      title: '客户列表',
      size:'huge',
      component: this.searchListTemp
    })
    .subscribe(() => {
      this.message.success('sucess')
    })
  }


  public delFun(groupno){
    this.modal
    .open({
      title: '确认删除',
      component: ModalConfirmComponent
    })
    .subscribe(() => {
      this.apiService.delGroup(groupno).subscribe(data => {
        this.message.success('删除成功')
        this.onRefresh()
      })

    })
  }
  transform(key:string,value:string,sql:string){
    if(sql != 'select *'){
      sql += ' and ' + key +" = " + "'*" + value + "*'"
    }
    else{
      sql += " where " + key +" = " + "'*" + value + "*'";
    }
    return sql
  }
  save(){
    this.apiService.updateGroup( this.groupFormData['data'],this.groupno).subscribe(data => {
      this.message.success('修改成功')
    })
    this.attrDialog = false;
    this.onRefresh()
  }
}
